import * as Cesium from 'cesium';

/**
 * Demo-specific utilities for PolygonAnimationDemo
 * These are only used in this specific demo and don't belong in the general utilities
 */

/**
 * Create a glowing polygon for the background
 * @param {Cesium.Viewer} viewer - The Cesium viewer instance
 * @param {Object} options - Polygon options
 */
export function createGlowPolygon(viewer, options) {
  const {
    positions,
    color = new Cesium.Color(0.2, 0.8, 1.0, 0.1),
    name = 'Glow Polygon'
  } = options;
  
  return viewer.entities.add({
    name,
    polygon: {
      hierarchy: Cesium.Cartesian3.fromDegreesArray(positions),
      material: color,
      height: 0,
      extrudedHeight: 50000
    }
  });
}

/**
 * Create multiple animated polygons with different animation patterns
 * @param {Cesium.Viewer} viewer - The Cesium viewer instance
 * @param {Array} polygonData - Array of polygon configurations
 */
export function createMultipleAnimatedPolygons(viewer, polygonData) {
  return polygonData.map(data => {
    const {
      positions,
      color,
      extrudedHeight,
      name,
      phase = 0
    } = data;
    
    return viewer.entities.add({
      name,
      polygon: {
        hierarchy: Cesium.Cartesian3.fromDegreesArray(positions),
        material: new Cesium.ColorMaterialProperty(
          new Cesium.CallbackProperty(() => {
            // 使用Date.now()来获取实时时间，确保动画持续进行
            let currentTime = Date.now();
            let alpha = 0.5 + 0.2 * Math.sin(currentTime * 0.005 + phase);
            return new Cesium.Color(
              color.red,
              color.green,
              color.blue,
              alpha
            );
          }, false) // isConstant = false
        ),
        outline: true,
        outlineColor: Cesium.Color.WHITE,
        height: 0,
        extrudedHeight
      }
    });
  });
}

/**
 * Create ripple effect animation
 * @param {Cesium.Viewer} viewer - The Cesium viewer instance
 * @param {Object} options - Ripple effect options
 * @param {Number} options.longitude - Center longitude
 * @param {Number} options.latitude - Center latitude
 * @param {Cesium.Color} options.color - Ripple color
 * @param {Number} options.maxRadius - Maximum radius of the ripple
 * @param {Number} options.speed - Animation speed
 * @param {Number} options.count - Number of ripples
 */
export function createRippleAnimation(viewer, options) {
  const {
    longitude,
    latitude,
    color = Cesium.Color.CYAN,
    maxRadius = 2,
    speed = 0.5,
    count = 5,
    name = 'Ripple Effect'
  } = options;

  const startTime = Date.now();

  // Create multiple ripple polygons
  const ripples = [];
  for (let i = 0; i < count; i++) {
    const ripple = viewer.entities.add({
      name: `${name} ${i + 1}`,
      polygon: {
        hierarchy: new Cesium.CallbackProperty(() => {
          const elapsed = (Date.now() - startTime) * 0.001; // Convert to seconds
          const progress = ((elapsed * speed - i * 0.5) % (count * 0.5)) / (count * 0.5);
          
          if (progress <= 0 || progress >= 1) {
            return undefined; // Hide the ripple when not active
          }
          
          // Calculate current radius
          const currentRadius = progress * maxRadius;
          
          // Create a circle polygon
          const positions = [];
          const sides = 32; // Number of sides for the circle
          for (let j = 0; j < sides; j++) {
            const angle = (j / sides) * Math.PI * 2;
            const lon = longitude + (currentRadius * Math.cos(angle)) / 111.32; // Approximate degrees
            const lat = latitude + (currentRadius * Math.sin(angle)) / 110.57; // Approximate degrees
            positions.push(lon, lat);
          }
          
          return new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArray(positions));
        }, false),
        material: new Cesium.ColorMaterialProperty(
          new Cesium.CallbackProperty(() => {
            const elapsed = (Date.now() - startTime) * 0.001;
            const progress = ((elapsed * speed - i * 0.5) % (count * 0.5)) / (count * 0.5);
            
            if (progress <= 0 || progress >= 1) {
              return new Cesium.Color(0, 0, 0, 0); // Transparent when not active
            }
            
            // Fade out as it expands
            const alpha = (1 - progress) * 0.5;
            return new Cesium.Color(
              color.red,
              color.green,
              color.blue,
              alpha
            );
          }, false)
        ),
        height: 0
      }
    });
    
    ripples.push(ripple);
  }
  
  return ripples;
}

export default {
  createGlowPolygon,
  createMultipleAnimatedPolygons,
  createRippleAnimation
};